/* conio.h library for large systems - small embedded systems use clibrary.c instead */
#include "../interpreter.h"

#ifndef BUILTIN_MINI_STDLIB

#include "iup.h"
#include "iup_config.h"
#include "pointerList.h"
#include "parseFunction.h"
#include <stdarg.h>


static Picoc *_pc = NULL;


void StdiupOpen(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Integer = IupOpen((int*)Param[0]->Val->Pointer,(char***)Param[1]->Val->Pointer);
}

void StdiupClose(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    IupClose();
}

void StdiupMainLoop(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Integer = IupMainLoop();
}

void StdiupMessage(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    IupMessage((const char *)Param[0]->Val->Pointer, (const char *)Param[1]->Val->Pointer);
}

void StdiupGetDialog(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupGetDialog(Param[0]->Val->Pointer);
}

void StdiupDestroy(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    IupDestroy(Param[0]->Val->Pointer);
}

void StdiupGetDialogChild(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupGetDialogChild(Param[0]->Val->Pointer, Param[1]->Val->Pointer);
}

void StdiupShow(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Integer = IupShow(Param[0]->Val->Pointer);
}

void StdiupShowXY(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Integer = IupShowXY(Param[0]->Val->Pointer, Param[1]->Val->Integer, Param[2]->Val->Integer);
}

void StdiupPopup(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Integer = IupPopup(Param[0]->Val->Pointer, Param[1]->Val->Integer, Param[2]->Val->Integer);
}


void StdiupHide(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Integer = IupHide(Param[0]->Val->Pointer);
}

void StdiupSetAttribute(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    IupSetAttribute(Param[0]->Val->Pointer, Param[1]->Val->Pointer, Param[2]->Val->Pointer);
}

void StdiupGetAttribute(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupGetAttribute(Param[0]->Val->Pointer, Param[1]->Val->Pointer);
}

void StdiupSetGlobal(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    IupSetGlobal(Param[0]->Val->Pointer, Param[1]->Val->Pointer);
}

void StdiupGetGlobal(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupGetGlobal(Param[0]->Val->Pointer);
}



void StdiupDialog(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupDialog(Param[0]->Val->Pointer);
}

void StdiupButton(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupButton(Param[0]->Val->Pointer, Param[1]->Val->Pointer);
}


extern void PicocParse(Picoc *pc, const char *FileName, const char *Source, int SourceLen, int RunIt, int CleanupNow, int CleanupSource);
extern struct ValueType IntType;


static PointerToPointerList * _list_action = NULL;


void _setActionCallback(void *key, void *value)
{
	if(_list_action == NULL)
	{
		_list_action = newPointerToPointerList();
	}
	setOrAddPointerToPointerList(_list_action,key,value);
}

void *_getActionCallback(void *key)
{
	if(_list_action == NULL)
	{
		_list_action = newPointerToPointerList();
	}
	return getPointerToPointerList(_list_action,key);
}


int StdiupActionCallbackExitValue = 0;

int __actionCallback(Ihandle *ih, ...)
{
	char buf[512];
	StdiupActionCallbackExitValue = IUP_DEFAULT;

	FunctionTypedef *func = (FunctionTypedef *)_getActionCallback(ih);
	char * funcName = func->funcName;

	va_list arg_ptr;
	va_start(arg_ptr, ih);
	
	sprintf(buf, "__actionCallback_exit_value=%s((void*)0x%p", funcName, (void*)ih);

	int i;
	int len;
	for(i=1; i<func->paramNum; i++)
	{
		len = strlen(buf);
		if(func->paramType[i] == POINTER_TYPE)
		{
			sprintf(&buf[len], ", (void*)0x%p", va_arg(arg_ptr, void *));
		}
		else
		{
			sprintf(&buf[len], ", %d", va_arg(arg_ptr, int));
		}
	}

	len = strlen(buf);
	sprintf(&buf[len], ");");


	

	if (!VariableDefined(_pc, TableStrRegister(_pc, "__actionCallback_exit_value")))
	{
        VariableDefinePlatformVar(_pc, NULL, "__actionCallback_exit_value", &_pc->IntType, (union AnyValue *)&StdiupActionCallbackExitValue, TRUE);
	}
    
    PicocParse(_pc, "startup", buf, strlen(buf), TRUE, TRUE, FALSE);

	return StdiupActionCallbackExitValue;
}

int __actionCallback_void(Ihandle *ih)
{
	char buf[512];
	StdiupActionCallbackExitValue = IUP_DEFAULT;

	FunctionTypedef *func = (FunctionTypedef *)_getActionCallback(ih);
	char * funcName = func->funcName;
	
	sprintf(buf, "__actionCallback_exit_value=%s();", funcName);

	if (!VariableDefined(_pc, TableStrRegister(_pc, "__actionCallback_exit_value")))
	{
        VariableDefinePlatformVar(_pc, NULL, "__actionCallback_exit_value", &_pc->IntType, (union AnyValue *)&StdiupActionCallbackExitValue, TRUE);
	}
    
    PicocParse(_pc, "startup", buf, strlen(buf), TRUE, TRUE, FALSE);

	return StdiupActionCallbackExitValue;
}






void StdiupGetCallback(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = (void*) _getActionCallback(Param[0]->Val->Pointer);
}

void StdiupSetCallback(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
	FunctionTypedef *func = parseFunction((const char *) Param[2]->Val->Pointer);
	
	_setActionCallback(Param[0]->Val->Pointer, (void*)func);
	
	if(func->paramNum == 1)
	{
		if(func->paramType[0] == VOID_TYPE)
		{
			IupSetCallback(Param[0]->Val->Pointer, Param[1]->Val->Pointer, (Icallback)__actionCallback_void);
		}
		else
		{
    		IupSetCallback(Param[0]->Val->Pointer, Param[1]->Val->Pointer, (Icallback)__actionCallback);
		}
	}
	else
	{
		IupSetCallback(Param[0]->Val->Pointer, Param[1]->Val->Pointer, (Icallback)__actionCallback);
	}
}



#define MAX_IUP_ARGS 16
Ihandle *StdiupBaseVbox(struct ParseState *Parser, void * ih, struct Value **Param, int NumArgs)
{
    struct Value *ThisArg = Param[0];
    int ArgCount = 0;
    void *ScanfArg[MAX_IUP_ARGS];
    
    if (NumArgs > MAX_IUP_ARGS)
        ProgramFail(Parser, "too many arguments to IupVbox() - %d max", MAX_IUP_ARGS);
    
    for (ArgCount = 0; ArgCount < NumArgs; ArgCount++)
    {
        ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
        
        if (ThisArg->Typ->Base == TypePointer) 
            ScanfArg[ArgCount] = ThisArg->Val->Pointer;
        
        else if (ThisArg->Typ->Base == TypeArray)
            ScanfArg[ArgCount] = &ThisArg->Val->ArrayMem[0];
        
        else
            ProgramFail(Parser, "non-pointer argument to IupVbox() - argument %d after format", ArgCount+1);
    }
	for (; ArgCount < MAX_IUP_ARGS; ArgCount++)
	{
		ScanfArg[ArgCount] = NULL;
	}
    
    return IupVbox((Ihandle*)ih, ScanfArg[0], ScanfArg[1], ScanfArg[2], ScanfArg[3], ScanfArg[4], ScanfArg[5], ScanfArg[6], ScanfArg[7], ScanfArg[8], ScanfArg[9],
		ScanfArg[10], ScanfArg[11], ScanfArg[12], ScanfArg[13], ScanfArg[14], ScanfArg[15]);
}


void StdiupVbox(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
    ReturnValue->Val->Pointer = StdiupBaseVbox(Parser, Param[0]->Val->Pointer, Param, NumArgs-1);
}

Ihandle *StdiupBaseHbox(struct ParseState *Parser, void * ih, struct Value **Param, int NumArgs)
{
    struct Value *ThisArg = Param[0];
    int ArgCount = 0;
    void *ScanfArg[MAX_IUP_ARGS];
    
    if (NumArgs > MAX_IUP_ARGS)
        ProgramFail(Parser, "too many arguments to IupHbox() - %d max", MAX_IUP_ARGS);
    
    for (ArgCount = 0; ArgCount < NumArgs; ArgCount++)
    {
        ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
        
        if (ThisArg->Typ->Base == TypePointer) 
            ScanfArg[ArgCount] = ThisArg->Val->Pointer;
        
        else if (ThisArg->Typ->Base == TypeArray)
            ScanfArg[ArgCount] = &ThisArg->Val->ArrayMem[0];
        
        else
            ProgramFail(Parser, "non-pointer argument to IupHbox() - argument %d after format", ArgCount+1);
    }
	for (; ArgCount < MAX_IUP_ARGS; ArgCount++)
	{
		ScanfArg[ArgCount] = NULL;
	}
    
    return IupHbox((Ihandle*)ih, ScanfArg[0], ScanfArg[1], ScanfArg[2], ScanfArg[3], ScanfArg[4], ScanfArg[5], ScanfArg[6], ScanfArg[7], ScanfArg[8], ScanfArg[9],
		ScanfArg[10], ScanfArg[11], ScanfArg[12], ScanfArg[13], ScanfArg[14], ScanfArg[15]);
}


void StdiupHbox(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
    ReturnValue->Val->Pointer = StdiupBaseHbox(Parser, Param[0]->Val->Pointer, Param, NumArgs-1);
}

Ihandle *StdiupBaseGridBox(struct ParseState *Parser, void * ih, struct Value **Param, int NumArgs)
{
    struct Value *ThisArg = Param[0];
    int ArgCount = 0;
    void *ScanfArg[MAX_IUP_ARGS];
    
    if (NumArgs > MAX_IUP_ARGS)
        ProgramFail(Parser, "too many arguments to IupGridBox() - %d max", MAX_IUP_ARGS);
    
    for (ArgCount = 0; ArgCount < NumArgs; ArgCount++)
    {
        ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
        
        if (ThisArg->Typ->Base == TypePointer) 
            ScanfArg[ArgCount] = ThisArg->Val->Pointer;
        
        else if (ThisArg->Typ->Base == TypeArray)
            ScanfArg[ArgCount] = &ThisArg->Val->ArrayMem[0];
        
        else
            ProgramFail(Parser, "non-pointer argument to IupGridBox() - argument %d after format", ArgCount+1);
    }
	for (; ArgCount < MAX_IUP_ARGS; ArgCount++)
	{
		ScanfArg[ArgCount] = NULL;
	}
    
    return IupGridBox((Ihandle*)ih, ScanfArg[0], ScanfArg[1], ScanfArg[2], ScanfArg[3], ScanfArg[4], ScanfArg[5], ScanfArg[6], ScanfArg[7], ScanfArg[8], ScanfArg[9],
		ScanfArg[10], ScanfArg[11], ScanfArg[12], ScanfArg[13], ScanfArg[14], ScanfArg[15]);
}


void StdiupGridBox(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
    ReturnValue->Val->Pointer = StdiupBaseGridBox(Parser, Param[0]->Val->Pointer, Param, NumArgs-1);
}

Ihandle *StdiupBaseMenu(struct ParseState *Parser, void * ih, struct Value **Param, int NumArgs)
{
    struct Value *ThisArg = Param[0];
    int ArgCount = 0;
    void *ScanfArg[MAX_IUP_ARGS];
    
    if (NumArgs > MAX_IUP_ARGS)
        ProgramFail(Parser, "too many arguments to IupGridBox() - %d max", MAX_IUP_ARGS);
    
    for (ArgCount = 0; ArgCount < NumArgs; ArgCount++)
    {
        ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
        
        if (ThisArg->Typ->Base == TypePointer) 
            ScanfArg[ArgCount] = ThisArg->Val->Pointer;
        
        else if (ThisArg->Typ->Base == TypeArray)
            ScanfArg[ArgCount] = &ThisArg->Val->ArrayMem[0];
        
        else
            ProgramFail(Parser, "non-pointer argument to IupGridBox() - argument %d after format", ArgCount+1);
    }
	for (; ArgCount < MAX_IUP_ARGS; ArgCount++)
	{
		ScanfArg[ArgCount] = NULL;
	}
    
    return IupMenu((Ihandle*)ih, ScanfArg[0], ScanfArg[1], ScanfArg[2], ScanfArg[3], ScanfArg[4], ScanfArg[5], ScanfArg[6], ScanfArg[7], ScanfArg[8], ScanfArg[9],
		ScanfArg[10], ScanfArg[11], ScanfArg[12], ScanfArg[13], ScanfArg[14], ScanfArg[15]);
}


void StdiupMenu(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
	ReturnValue->Val->Pointer = StdiupBaseMenu(Parser, Param[0]->Val->Pointer, Param, NumArgs-1);
}


void StdiupLabel(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
    ReturnValue->Val->Pointer = IupLabel(Param[0]->Val->Pointer);
}

void StdiupImage(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
	ReturnValue->Val->Pointer = IupImage(Param[0]->Val->Integer,Param[1]->Val->Integer,Param[2]->Val->Pointer);
}

void StdiupImageRGB(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
    ReturnValue->Val->Pointer = IupImageRGB(Param[0]->Val->Integer,Param[1]->Val->Integer,Param[2]->Val->Pointer);
}

void StdiupImageRGBA(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
    ReturnValue->Val->Pointer = IupImageRGBA(Param[0]->Val->Integer,Param[1]->Val->Integer,Param[2]->Val->Pointer);
}

void StdiupSetHandle(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
    ReturnValue->Val->Pointer = IupSetHandle(Param[0]->Val->Pointer,Param[1]->Val->Pointer);
}

void StdiupSetAttributeHandle(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
    IupSetAttributeHandle(Param[0]->Val->Pointer,Param[1]->Val->Pointer, Param[2]->Val->Pointer);
}

void StdiupGetAttributeHandle(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
    ReturnValue->Val->Pointer = IupGetAttributeHandle(Param[0]->Val->Pointer,Param[1]->Val->Pointer);
}


void StdiupConfig(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
    ReturnValue->Val->Pointer = IupConfig();
}

void StdiupConfigLoad(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
    ReturnValue->Val->Integer = IupConfigLoad(Param[0]->Val->Pointer);
}

void StdiupConfigSave(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
    ReturnValue->Val->Integer = IupConfigSave(Param[0]->Val->Pointer);
}

void StdiupConfigDialogShow(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
    IupConfigDialogShow(Param[0]->Val->Pointer, Param[1]->Val->Pointer, Param[2]->Val->Pointer);
}

void StdiupConfigDialogClosed(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
    IupConfigDialogClosed(Param[0]->Val->Pointer, Param[1]->Val->Pointer, Param[2]->Val->Pointer);
}


void StdiupText(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupText(Param[0]->Val->Pointer);
}

void StdiupTimer(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupTimer();
}

void StdiupList(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupList(Param[0]->Val->Pointer);
}

void StdiupToggle(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupToggle(Param[0]->Val->Pointer, Param[1]->Val->Pointer);
}

void StdiupProgressBar(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupProgressBar();
}

void StdiupVal(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupVal(Param[0]->Val->Pointer);
}

void StdiupSpace(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupSpace();
}

void StdiupScrollBox(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupScrollBox(Param[0]->Val->Pointer);
}

void StdiupFrame(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupFrame(Param[0]->Val->Pointer);
}

void StdiupItem(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupItem(Param[0]->Val->Pointer,Param[1]->Val->Pointer );
}

void StdiupSubmenu(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupSubmenu(Param[0]->Val->Pointer,Param[1]->Val->Pointer );
}

void StdiupSeparator(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupSeparator();
}

void StdiupGetInt(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Integer = IupGetInt(Param[0]->Val->Pointer,Param[1]->Val->Pointer);
}

void StdiupFileDlg(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupFileDlg();
}

void StdiupMessageDlg(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupMessageDlg();
}

void StdiupColorDlg(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupColorDlg();
}

void StdiupFontDlg(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupFontDlg();
}

void StdiupProgressDlg(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupProgressDlg();
}

void StdiupCanvas(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupCanvas(Param[0]->Val->Pointer);
}

void StdiupTree(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupTree();
}

void StdiupDatePick(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupDatePick();
}

void StdiupCalendar(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupCalendar();
}

void StdiupColorbar(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupColorbar();
}

void StdiupGauge(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupGauge();
}

void StdiupDial(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupDial(Param[0]->Val->Pointer);
}

void StdiupColorBrowser(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupColorBrowser();
}

void StdiupSetAttributes(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupSetAttributes(Param[0]->Val->Pointer, Param[1]->Val->Pointer);
}

void StdiupGetHandle(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupGetHandle(Param[0]->Val->Pointer);
}

void StdiupSetLanguage(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    IupSetLanguage(Param[0]->Val->Pointer);
}

void StdiupGetLanguage(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupGetLanguage();
}

void StdiupSetLanguageString(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    IupSetLanguageString(Param[0]->Val->Pointer, Param[1]->Val->Pointer);
}

void StdiupStoreLanguageString(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    IupStoreLanguageString(Param[0]->Val->Pointer, Param[1]->Val->Pointer);
}

void StdiupGetLanguageString(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupGetLanguageString(Param[0]->Val->Pointer);
}

void StdiupMultiLine(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) 
{
    ReturnValue->Val->Pointer = IupMultiLine(Param[0]->Val->Pointer);
}




/* handy structure definitions */
const char StdiupDefs[] = "\
typedef void* Ihandle; \
";




/* all conio functions */
struct LibraryFunction StdiupFunctions[] =
{
    { StdiupOpen,   			"int IupOpen(int *argc, char ***argv);" },
    { StdiupClose,  			"void IupClose(void);" },
    { StdiupMainLoop,   		"int IupMainLoop(void);" },
    { StdiupMessage,   			"void IupMessage(char *title, char *msg);" },
    { StdiupGetDialog, 			"Ihandle* IupGetDialog(Ihandle* ih);" },
    { StdiupDestroy,   			"void IupDestroy(Ihandle* ih);" },
    { StdiupGetDialogChild,   	"Ihandle* IupGetDialogChild(Ihandle* ih, char* name);" },
    { StdiupShow,   			"int IupShow(Ihandle* ih);" },
    { StdiupShowXY,   			"int IupShowXY(Ihandle* ih, int x, int y);"},
    { StdiupHide,   			"int IupHide(Ihandle* ih);"},
    { StdiupPopup,   			"int IupPopup(Ihandle* ih, int x, int y);" },
    { StdiupSetAttribute,   	"void IupSetAttribute(Ihandle* ih, char* name, char* value);" },
    { StdiupGetAttribute,   	"char* IupGetAttribute(Ihandle* ih, char* name);" },
    { StdiupSetGlobal,   		"void IupSetGlobal(char* name, char* value);" },
    { StdiupGetGlobal,   		"char* IupGetGlobal(char* name);" },
    { StdiupGetCallback,   		"char* IupGetCallback(Ihandle* ih, char *name);" },
    { StdiupSetCallback,   		"void  IupSetCallback(Ihandle* ih, char *name, char* funcName);" },
    { StdiupDialog,   			"Ihandle* IupDialog(Ihandle* child);" },
    { StdiupButton,   			"Ihandle* IupButton(char* title, char* action);" },
    { StdiupVbox,   			"Ihandle* IupVbox(Ihandle* child, ...);" },
    { StdiupHbox,   			"Ihandle* IupHbox(Ihandle* child, ...);" },
    { StdiupGridBox,   			"Ihandle* IupGridBox(Ihandle* child, ...);" },
    { StdiupMenu,   			"Ihandle* IupMenu(Ihandle* child, ...);" },
    { StdiupLabel,   			"Ihandle* IupLabel(char* title);" },
    { StdiupImage,   			"Ihandle* IupImage(int width, int height, unsigned char *pixmap);" },
    { StdiupImageRGB,   		"Ihandle* IupImageRGB(int width, int height, unsigned char *pixmap);" },
    { StdiupImageRGBA,   		"Ihandle* IupImageRGBA(int width, int height, unsigned char *pixmap);" },
    { StdiupSetHandle,   		"Ihandle* IupSetHandle(char *name, Ihandle* ih);" },
    { StdiupSetAttributeHandle, "void IupSetAttributeHandle(Ihandle* ih, char* name, Ihandle* ih_named);" },
	{ StdiupGetAttributeHandle, "Ihandle* IupGetAttributeHandle(Ihandle* ih, char* name);"},
    { StdiupConfig,   			"Ihandle* IupConfig(void);" },
	{ StdiupConfigLoad,   		"int IupConfigLoad(Ihandle* ih);" },
	{ StdiupConfigSave,   		"int IupConfigSave(Ihandle* ih);" },
	{ StdiupConfigDialogShow,   "void IupConfigDialogShow(Ihandle* ih, Ihandle* dialog, char* name);"},
	{ StdiupConfigDialogClosed, "void IupConfigDialogClosed(Ihandle* ih, Ihandle* dialog, char* name);" },
	{ StdiupTimer, 				"Ihandle* IupTimer(void);" },
	{ StdiupText, 				"Ihandle* IupText(char* action);" },
	{ StdiupList, 				"Ihandle* IupList(char* action);" },
	{ StdiupToggle, 			"Ihandle* IupToggle(char* title, char* action);" },
	{ StdiupProgressBar, 		"Ihandle* IupProgressBar(void);" },
	{ StdiupVal, 				"Ihandle* IupVal(char *type);" },
	{ StdiupSpace, 				"Ihandle* IupSpace(void);" },
	{ StdiupScrollBox, 			"Ihandle* IupScrollBox(Ihandle* child);" },
	{ StdiupFrame, 				"Ihandle* IupFrame(Ihandle* child);" },
	{ StdiupItem, 				"Ihandle* IupItem(char* title, char* action);" },
	{ StdiupSubmenu, 			"Ihandle* IupSubmenu(char* title, Ihandle* child);"},
	{ StdiupSeparator, 			"Ihandle* IupSeparator(void);" },
	{ StdiupGetInt, 			"int IupGetInt(Ihandle* ih, char* name);" },
    { StdiupFileDlg, 			"Ihandle* IupFileDlg(void);" },
    { StdiupMessageDlg, 		"Ihandle* IupMessageDlg(void);" },
    { StdiupColorDlg, 			"Ihandle* IupColorDlg(void);" },
    { StdiupFontDlg, 			"Ihandle* IupFontDlg(void);" },
    { StdiupProgressDlg, 		"Ihandle* IupProgressDlg(void);" },
    { StdiupCanvas, 			"Ihandle* IupCanvas(char* action);" },
    { StdiupTree, 				"Ihandle* IupTree(void);" },
    { StdiupDatePick, 			"Ihandle* IupDatePick(void);" },
    { StdiupCalendar, 			"Ihandle* IupCalendar(void);" },
	{ StdiupColorbar, 			"Ihandle* IupColorbar(void);" },
	{ StdiupGauge, 				"Ihandle* IupGauge(void);" },
	{ StdiupDial, 				"Ihandle* IupDial(char* type);" },
	{ StdiupColorBrowser, 		"Ihandle* IupColorBrowser(void);" },
	{ StdiupSetAttributes, 		"Ihandle* IupSetAttributes(Ihandle* ih, char *str);"},
	{ StdiupGetHandle, 			"Ihandle* IupGetHandle(char *name);" },
	{ StdiupSetLanguage, 		"void IupSetLanguage(char *lng);" },
	{ StdiupGetLanguage, 		"char* IupGetLanguage(void);" },
	{ StdiupSetLanguageString, 	"void IupSetLanguageString(char* name, char* str);" },
	{ StdiupStoreLanguageString, "void IupStoreLanguageString(char* name, char* str);" },
	{ StdiupGetLanguageString, 	"char* IupGetLanguageString(char* name);"},
	{ StdiupMultiLine, 			"Ihandle* IupMultiLine(char* action);" },
    
    
    { NULL,         NULL }
};

static int ZeroValue = 0;

/************************************************************************/
/*                   Callback Return Values                             */
/************************************************************************/
static int IUP_IGNOREValue 	= IUP_IGNORE;
static int IUP_DEFAULTValue = IUP_DEFAULT;
static int IUP_CLOSEValue	= IUP_CLOSE;
static int IUP_CONTINUEValue =IUP_CONTINUE;

/************************************************************************/
/*           IupPopup and IupShowXY Parameter Values                    */
/************************************************************************/
static int IUP_CENTERValue 	=        IUP_CENTER;  /* 65535 */
static int IUP_LEFTValue 	=        IUP_LEFT;  /* 65534 */
static int IUP_RIGHTValue 	=        IUP_RIGHT;  /* 65533 */
static int IUP_MOUSEPOSValue 	=    IUP_MOUSEPOS;  /* 65532 */
static int IUP_CURRENTValue 	=    IUP_CURRENT;  /* 65531 */
static int IUP_CENTERPARENTValue 	=IUP_CENTERPARENT;  /* 65530 */
static int IUP_TOPValue 	=        IUP_TOP;
static int IUP_BOTTOMValue 	=        IUP_BOTTOM;


/************************************************************************/
/*                   Common Flags and Return Values                     */
/************************************************************************/
static int IUP_ERRORValue = IUP_ERROR;
static int IUP_NOERRORValue = IUP_NOERROR;
static int IUP_OPENEDValue = IUP_OPENED;
static int IUP_INVALIDValue = IUP_INVALID;
static int IUP_INVALID_IDValue = IUP_INVALID_ID;

/************************************************************************/
/*               Mouse Button Values and Macros                         */
/************************************************************************/
static int IUP_BUTTON1Value = IUP_BUTTON1;
static int IUP_BUTTON2Value = IUP_BUTTON2;
static int IUP_BUTTON3Value = IUP_BUTTON3;
static int IUP_BUTTON4Value = IUP_BUTTON4;
static int IUP_BUTTON5Value = IUP_BUTTON5;


/* creates various system-dependent definitions */
void StdiupSetupFunc(Picoc *pc)
{ 
	_pc = pc;
	/* make a "struct Ihandle_" which is the same size as a native Ihandle_ structure */
    TypeCreateOpaqueStruct(pc, NULL, TableStrRegister(pc, "Ihandle"), sizeof(void*));

    /* define NULL, TRUE and FALSE */
    if (!VariableDefined(pc, TableStrRegister(pc, "NULL")))
    {
        VariableDefinePlatformVar(pc, NULL, "NULL", &pc->IntType, (union AnyValue *)&ZeroValue, FALSE);
    }
	VariableDefinePlatformVar(pc, NULL, "EXIT_SUCCESS", &pc->IntType, (union AnyValue *)&ZeroValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_IGNORE", &pc->IntType, (union AnyValue *)&IUP_IGNOREValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_DEFAULT", &pc->IntType, (union AnyValue *)&IUP_DEFAULTValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_CLOSE", &pc->IntType, (union AnyValue *)&IUP_CLOSEValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_CONTINUE", &pc->IntType, (union AnyValue *)&IUP_CONTINUEValue, FALSE);
	
	VariableDefinePlatformVar(pc, NULL, "IUP_CENTER", &pc->IntType, (union AnyValue *)&IUP_CENTERValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_LEFT", &pc->IntType, (union AnyValue *)&IUP_LEFTValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_RIGHT", &pc->IntType, (union AnyValue *)&IUP_RIGHTValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_MOUSEPOS", &pc->IntType, (union AnyValue *)&IUP_MOUSEPOSValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_CURRENT", &pc->IntType, (union AnyValue *)&IUP_CURRENTValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_CENTERPARENT", &pc->IntType, (union AnyValue *)&IUP_CENTERPARENTValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_TOP", &pc->IntType, (union AnyValue *)&IUP_TOPValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_BOTTOM", &pc->IntType, (union AnyValue *)&IUP_BOTTOMValue, FALSE);

	VariableDefinePlatformVar(pc, NULL, "IUP_ERROR", &pc->IntType, (union AnyValue *)&IUP_ERRORValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_NOERROR", &pc->IntType, (union AnyValue *)&IUP_NOERRORValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_OPENED", &pc->IntType, (union AnyValue *)&IUP_OPENEDValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_INVALID", &pc->IntType, (union AnyValue *)&IUP_INVALIDValue, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_INVALID_ID", &pc->IntType, (union AnyValue *)&IUP_INVALID_IDValue, FALSE);

	VariableDefinePlatformVar(pc, NULL, "IUP_BUTTON1", &pc->IntType, (union AnyValue *)&IUP_BUTTON1Value, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_BUTTON2", &pc->IntType, (union AnyValue *)&IUP_BUTTON2Value, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_BUTTON3", &pc->IntType, (union AnyValue *)&IUP_BUTTON3Value, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_BUTTON4", &pc->IntType, (union AnyValue *)&IUP_BUTTON4Value, FALSE);
	VariableDefinePlatformVar(pc, NULL, "IUP_BUTTON5", &pc->IntType, (union AnyValue *)&IUP_BUTTON5Value, FALSE);
}


#endif

